package com.ydlclass.ChannelHandler.handler.InboundHandler;

import com.ydlclass.Transport.message.MessageFormatConstant;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 基于长度字段的帧解码器
 * maxFrameLength 最大帧长度
 * lengthFieldOffset 长度字段偏移量
 * lengthFieldLength 长度字段长度
 * lengthAdjustment 调整
 * initialBytesToStrip 丢弃的字节数
 * @Author duWenJian
 * @Date 2024/2/21 11:12
 * @Version 1.0
 */
public class ProtocolFrameDecoder extends LengthFieldBasedFrameDecoder {
    public ProtocolFrameDecoder() {
        this(
                // 最大帧长度
                MessageFormatConstant.MAX_FRAME_LENGTH,
                // 长度字段偏移量
                MessageFormatConstant.MAGIC.length + MessageFormatConstant.VERSION_LENGTH + MessageFormatConstant.HEAD_FIELD_LENGTH,
                // 长度字段长度
                MessageFormatConstant.FULL_FIELD_LENGTH,
                // 调整
                -(MessageFormatConstant.MAGIC.length + MessageFormatConstant.VERSION_LENGTH + MessageFormatConstant.HEAD_FIELD_LENGTH +
                MessageFormatConstant.FULL_FIELD_LENGTH),
                // 丢弃的字节数
                0);
    }

    public ProtocolFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
    }
}
